package aishua.com.mytestapplist.datastructure;

/**
 * Created by aishua on 2018/4/27.
 */

public class LinkStack<T> {
    private static final String TAG = "LinkStack--";
    public class Node{

        public T data;
        public Node next;
        public Node(){

            data =null;
        }
        public Node(T data){

            this.data=data;
        }

    }

    private Node head;//保存第一个数据
    private Node temp;//新的结点
    private int size;//保存栈的大小

    public LinkStack(){
          head = null;
          temp = null;
          size =0;

    }

    public LinkStack(T data){

        head = new Node(data);
        size++;
    }

    public int length(){

        return size;
    }

    //入栈(头插法)
    public void push(T t){


        temp = new Node(t);
        temp.next = head;
        head = temp;
        size++;
    }


    //    出栈
    public T pop(){
        if (head ==null){
            throw new NullPointerException("栈为空");
        }
        T t = head.data;
        head= head.next;
        size--;
        return t;
    }
    public boolean isEmpty(){

        return head == null;
    }
}
